import {
  getRoleTable,
  getViewRoleTable,
  editRoleRecord,
  getRoleInfo
} from '../../api/system'
import {
  ROLE_LIST,
  ROLE_CURRENT_ROW,
  ROLE_DATA_COUNT,
  ROLE_HISTORY_DATA,
  ROLE_PAGE_SIZE,
  ROLE_LIST_DATA,
  ROLE_INFO_LIST
} from '../motations-type'

export default {
  state: {
    roleList: [],
    roleDataCount: null,
    rolePageSize: 10,
    roleHistoryData: [],
    roleListData: [],
    roleInfoList: [],
    row: {
      ctime: '',
      role: '',
      mtime: '',
      gname: '',
      compentence: []
    }
  },
  getters: {
    roleList: state => state.roleList
  },
  mutations: {
    [ROLE_LIST] (state, data) {
      state.roleList = data
    },
    [ROLE_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [ROLE_DATA_COUNT] (state, data) {
      state.roleDataCount = data
    },
    [ROLE_HISTORY_DATA] (state, data) {
      state.roleHistoryData = data
    },
    [ROLE_PAGE_SIZE] (state, data) {
      state.rolePageSize = data
    },
    [ROLE_LIST_DATA] (state, data) {
      state.roleListData = data
    },
    [ROLE_INFO_LIST] (state, data) {
      state.roleInfoList = data
    }
  },
  actions: {
    async getRoleList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getRoleTable(data)
      resp.data.data.map((item) => {
        newData = [...newData, {
          ctime: item.ctime,
          role: item.role,
          mtime: item.mtime,
          gname: item.gname,
          compentence: []
        }]
      })
      commit(ROLE_LIST, newData)
      commit(ROLE_DATA_COUNT, newData.length)
      if (newData.length < rootState.role.rolePageSize) {
        commit(ROLE_HISTORY_DATA, newData)
      } else {
        commit(ROLE_HISTORY_DATA, newData.slice(0, rootState.role.rolePageSize))
      }
      return resp
    },
    async getRoleViewList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let itemData = null
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getViewRoleTable(data)
      commit(ROLE_LIST_DATA, resp.data.data)
    },
    async getRoleInfoList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getRoleInfo(data)
      resp.data.data.map((item) => {
        if (item.hasRole === true) {
          newData.push(item.id)
          if (item.children) {
            item.children.map((item) => {
              if (item.hasRole === true) {
                newData.push(item.id)
              }
            })
          }
        }
      })
      commit(ROLE_INFO_LIST, newData)
    },
    async editRoleData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editRoleRecord(data)
      return resp
    },
    async getRoleCurrentRow ({ commit, rootState }, row) {
      commit(ROLE_CURRENT_ROW, row)
    },
    async changeRolePage ({ commit, rootState }, index) {
      let data = rootState.role.roleList
      let _start = (index - 1) * rootState.role.rolePageSize
      let _end = index * rootState.role.rolePageSize
      commit(ROLE_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeRolePageSize ({ commit, rootState }, value) {
      commit(ROLE_PAGE_SIZE, value)
    }
  }
}
